const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
  data: {
      list:[],
      id:0,
      money:0,
      payOk:false,
      payScore:0,
      status:0,
      imgUrl:app.globalData.imgUrl,
      info:{},
      bgTrue1:false,
      bgTrue2:false
  },
  onLoad: function(){
    this.getVip()
  },
  getVip:function(){
    var that = this
    var data ={}
    $api.get('vip/type',data).then(res => {
      //请求成功
      if(res.data.code == 1){
          that.setData({
            list:res.data.data
            //info:res.data.data[0]
          })
          that.getUserVip(res.data.data)
      }
    })
  },
  getUserVip:function(arr){
    var that = this
    var data ={uid:wx.getStorageSync('uid')}
    $api.get('vip/user',data).then(res => {
      //请求成功
      if(res.data.code == 1){
          that.setData({
            bgTrue1:res.data.data.bgTrue1,
            bgTrue2:res.data.data.bgTrue2
          })
          if(!res.data.data.bgTrue1){
            that.setData({
              id:2,
              info:arr[0],
              money:0
            })
          }else{
            if(!res.data.data.bgTrue2){
              that.setData({
                id:3,
                info:arr[1],
                money:arr[1]['score']
              })
            }else{
              that.setData({
                id:4,
                info:arr[2],
                money:arr[2]['price']
              })
            }
          }
      }
    })
  },
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  order:function(e){
    var index = e.currentTarget.dataset.index
    if(this.data.bgTrue1 && this.data.list[index]['id'] == 2){
      wx.showToast({
        title:'只限新注册会员可领取,且只可领取一次,不可重复领取!!!',
        duration:2000,
        mask:true, 
        icon:'none',
      })
      return;
    }
    if(this.data.bgTrue2 && this.data.list[index]['id'] == 3){
      wx.showToast({
        title:'所有会员只可积分兑换一次,不可重复兑换!!!',
        duration:2000,
        mask:true, 
        icon:'none',
      })
      return;
    }
    if(this.data.list[index]['id'] == 3){
      var money = this.data.list[index].score
    }else{
      var money = this.data.list[index]['price']
    }
    this.setData({
      id:this.data.list[index]['id'],
      money:money
      // info:this.data.list[index]
    })
    this.payquery()
  },
  payquery:function(id){
    var that = this
    var data = {'uid':wx.getStorageSync('uid')}
    $api.get('member/info',data).then(res =>{
      //请求成功
      if(res.data.code == 1){
        that.getCount(res.data.data)
      }
    })
  },
  payclose:function(){
    this.setData({
      payOk:false
    })
  },
  getCount:function(info){
    if(info.status == 3){
      //审核通过 可以使用积分 和余额支付 有限使用积分
      if(this.data.id == 2){
        var payScore = 0
        var payMoney = 0
        //var payRed = 0
      }else if(this.data.id == 3){
        if(parseFloat(this.data.money) > parseFloat(info.integral)){
          wx.showToast({
            title:'您的积分余额为'+info.integral+'积分不足,不能抵扣!!!',
            duration:2000,
            mask:true, 
            icon:'none',
          })
          return;
        }
        var payScore = this.data.list[1].score
        var payMoney = 0
        //var payRed = 0
      }else{
        var score = this.data.money/10 - info.integral;
        if(score > 0){
          //积分小于支付金额
          var payScore = info.integral
          var payMoney = this.data.money - info.integral
          // if(score - info.money > 0){//积分不够支付  判断余额
          //   var payRed = info.money;
          //   var payMoney = score - info.money
          // }else{
          //   var payRed = score
          //   var payMoney = 0
          // }
        }else{
          //积分大于金额 全部积分支付
          var payScore = this.data.money/10
          var payMoney = this.data.money/10*9
        }
      }
    }else{
      //审核未通过,只能使用人民币支付
      var payScore = 0
      //var payRed = 0
      var payMoney = this.data.money
    }

    this.setData({
      payOk:true,
     // payRed:payRed,
      payScore:payScore,
      status:info.status,
      payMoney:payMoney
    })
  },
  pay:function(){
    var data = {
      'uid':wx.getStorageSync('uid'),
      'payMoney':this.data.payMoney, //支付金额
      'payScore':this.data.payScore, //抵扣积分
      'money':this.data.money,//订单总价
      'vip_id':this.data.id
    }
    var that = this
    $api.post('wxpay/vipPay',data).then(res =>{
      if(res.data.code == 1){
          if(that.data.payMoney > 0){
              that.getPayResult(res);
          }else{
              if(that.data.id == 2){
                that.setData({
                  bgTrue1:true
                })
              }
              if(that.data.id == 3){
                that.setData({
                  bgTrue2:true
                })
              }
              that.setData({
                payOk:false,
                id:0
              })
              wx.showToast({
                  title:'兑换成功',
                  duration:3000,
                  mask:true, 
                  icon:'success',
                  success:function(){
                      setTimeout(function(){
                          wx.switchTab({
                            url: '../user/index'
                          })
                      },3000) //延迟时间 这里是
                  }
              })
          }
      }else{
          wx.showToast({
              title:res.data.msg,
              duration:3000,
              mask:true, 
              icon:'none',
          })
          return;
      }
    })
  },
  getPayResult:function(e){
    var that = this
    // 签权调起支付 
    wx.requestPayment({
        'timeStamp': e.data.data.timeStamp,
        'nonceStr': e.data.data.nonceStr,
        'package': e.data.data.package,
        'signType': e.data.data.signType,
        'paySign': e.data.data.paySign,
        'success': function (res){
            that.setData({
                payOk:false
            })
            that.payNotify(e.data.data.orderid)
        },
        'fail': function (res){
            //console.log("支付失败",res)
        },     
    })
  },
  payNotify:function(orderid){
    var data = {'orderid':orderid}
    $api.post('wxpay/payVipNotify',data).then(res => {
          if(res.data.code == 1){
            wx.showToast({
              title:'兑换成功',
              duration:3000,
              mask:true, 
              icon:'success',
              success:function(){
                  setTimeout(function(){
                      wx.switchTab({
                        url: '../user/index'
                      })
                  },3000) //延迟时间 这里是
              }
          })
          }
     })
},
});